package com.project.mvprxsample.ui.fragment;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.project.mvprxsample.R;
import com.project.mvprxsample.ui.activity.FragmentTestActivity;

import org.json.JSONObject;

public abstract class BaseFragment extends Fragment {
    private Context mContext;

    public OnFragmentInteractionListener mListener;

    public static final String GOBACK = "goBack";
    public static final String SETTITLE = "setTitle";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_fragment, container, false);
        FrameLayout content = (FrameLayout) view.findViewById(R.id.fragment);
        content.addView(inflater.inflate(getLayoutId(), null));
        initView(view, savedInstanceState);
        initTitle();
        initPresenters();
        return view;
    }

    /**
     * 获取fragment布局文件ID
     *
     * @return
     */
    protected abstract int getLayoutId();

    /**
     * 初始化控键
     *
     * @param view
     * @param savedInstanceState
     */
    protected abstract void initView(View view, Bundle savedInstanceState);

    /**
     * 初始化标题栏
     */
    protected abstract void initTitle();

    /**
     * 初始化presenters
     */
    protected abstract void initPresenters();


    /**
     * 添加fragment
     *
     * @param fragment
     * @ Exception
     */
    protected void addFragment(BaseFragment fragment) {
        try {
            if (null != fragment && mContext != null && (!fragment.isAdded())) {
                ((FragmentTestActivity) mContext).addFragment(fragment);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * replaceFragment
     *
     * @param fragment
     */
    protected void replaceFragment(BaseFragment fragment) {
        try {
            if (null != fragment && mContext != null) {
                ((FragmentTestActivity) mContext).replaceFragment(fragment);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 移除fragment
     *
     * @param fragment
     */
    protected void removeFragment(BaseFragment fragment) {
        try {
            if (mContext != null) {
                ((FragmentTestActivity) mContext).removeFragment(fragment);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(GOBACK, null);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(String tag, JSONObject json);
    }
}
